//
//  CorgiStatueOriginalView.swift
//  weibo
//
//  Created by Corgi on 17/3/24.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import SnapKit

class CorgiStatueOriginalView: UIView {
    
    var originalViewBottomConstraint: Constraint?

    var statusViewModel: CorgiStatusViewModel? {
        
        didSet {
            
            headImageView.corgi_setImage(urlString: statusViewModel?.homeModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            
            membershipImageView.image = statusViewModel?.mbrankImage
            
            avatarImageView.image = statusViewModel?.verifiedImage
            
            //contentLabel.text = statusViewModel?.homeModel?.text
            
            contentLabel.attributedText = statusViewModel?.orginalAttributedString
            
            originalViewBottomConstraint?.deactivate()
            
            if let picUrls = statusViewModel?.homeModel?.pic_urls, picUrls.count > 0 {
                
                pictureView.picUrls = picUrls
                
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HOMECELLMARGIN).constraint
                })
                
                pictureView.isHidden = false
                
            } else {
                
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HOMECELLMARGIN).constraint
                })
                
                pictureView.isHidden = true
            }
            //微博时间&来源
            sourceLabel.text = statusViewModel?.sourceText
            timeLabel.text = statusViewModel?.createTime
            
        }
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //backgroundColor = randomColor()
        backgroundColor = UIColor.white
        
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImageView.snp.makeConstraints { (make) in
            make.leading.top.equalToSuperview().offset(HOMECELLMARGIN)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView.snp.right).offset(HOMECELLMARGIN)
            make.top.equalTo(headImageView)
        }
        
        membershipImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp.right).offset(HOMECELLMARGIN)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(HOMECELLMARGIN)
            make.top.equalTo(timeLabel)
        }
        
        avatarImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(HOMECELLMARGIN)
            make.width.equalTo(screenWidth - 2 * HOMECELLMARGIN)
        }
        
        contentLabel.textAlignment = .left
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(HOMECELLMARGIN)
            make.top.equalTo(contentLabel.snp.bottom).offset(HOMECELLMARGIN)
        }
        
        self.snp.makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HOMECELLMARGIN).constraint
        }
        
    }

    private lazy var headImageView: UIImageView = UIImageView(imageName: "avatar_default")
    
    private lazy var nameLabel: UILabel = UILabel(title: "昵称", textColor: UIColor.black, fontSize: FONTSIZEOFNORMAL)
    
    private lazy var membershipImageView: UIImageView = UIImageView(imageName: "common_icon_membership")
    
    private lazy var timeLabel: UILabel = UILabel(title: "微博时间", textColor: THEMECOLOR, fontSize: FONTSIZEOFSMALL)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "微博来源", textColor: UIColor.darkGray, fontSize: FONTSIZEOFSMALL)
    // 微博认证
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文", textColor: UIColor.black, fontSize: FONTSIZEOFNORMAL)
    
    
    //图片
    private lazy var pictureView: CorgiStatusPictureView = CorgiStatusPictureView()
    
}





